{% load static %}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>{% block title %}{% endblock %}</title>

    <link rel="stylesheet" href="{% static 'base.css' %}">
    <link rel="stylesheet" href="{% static 'bootstrap-3.3.7/css/bootstrap.min.css' %}">
    <script type="text/javascript" src="{% static 'jquery-1.12.4.min.js' %}"></script>
    <script type="text/javascript" src="{% static 'bootstrap-3.3.7/js/bootstrap.min.js' %}"></script>

    {% block header_extends %}{% endblock %}
</head>

<body>
    <nav class="navbar navbar-default" id="navigation">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="{% url 'home' %}">良言</a>
                <button class="navbar-toggle collapsed" data-toggle="collapse" data-target="
                #navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <div id="navbar-collapse" class="collapse navbar-collapse">
                <ul class="nav navbar-nav" id="navbar_left">
                    <li class="{% block nav_home_active %}{% endblock %}">
                        <a href="{% url 'home' %}">首页</a>
                    </li>
                    <li class="{% block nav_blog_active %}{% endblock %}">
                        <a href="{% url 'blog_list' %}">博客</a>
                    </li>
                </ul>

                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <form class="navbar-form search-form" action="{% url 'search' %}">
                            <div class="form-group">
                                <input type="text" name="wd" class="form-control" placeholder="Search">
                            </div>
                            <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search"></span></button>
                        </form>
                    </li>
                    {% if not user.is_authenticated %}
                        <li><a href="{% url 'login' %}?from={{ request.get_full_path }}">登录</a></li>
                        <li><a href="{% url 'register' %}?from={{ request.get_full_path }}">注册</a></li>
                    {% else %}
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">
                                {{ user.username }}
                                {% if user.get_nickname %}
                                    ({{ user.get_nickname }})
                                {% endif %}
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu">
                                <li><a href="{% url 'user_info' %}">个人信息</a></li>
                                {% if user.is_staff or user.is_superuser %}
                                    <li><a href="{% url 'admin:index' %}">后台管理</a></li>
                                {% endif %}
                                <li><a href="{% url 'reset_password' %}?from={{ request.get_full_path }}">修改密码</a></li>
                                <li><a href="{% url 'logout' %}?from={{ request.get_full_path }}">注销</a></li>
                            </ul>
                        </li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </nav>    
    {% block content %}{% endblock %}


    {# footer，显示网站相关信息 #}
    <div class="footer">
        <div class="website-infomation text-center">
            <p>京ICP备20026653号 Copyright ©2020 liangyan.fun 版权所有</p>
            <p>意见反馈或联系作者(1046678590@qq.com)</p>
        </div>
    </div>

    {# 模态登录窗口 #}
    <div class="modal fade" id="login_modal" tabindex="-1" role="dialog">
      <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <form id="modal_login_form" action="" autocomplete="on">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">登录</h4>
                </div>

                <div class="modal-body">
                    {% csrf_token %}
                    {% for field in modal_login_form %}
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    {{ field }}
                    {% endfor %}
                    <span class="pull-left text-danger" id="modal_login_error"></span>
                </div>

                <div class="modal-footer">
                    <a href="{% url 'retrieve_password' %}?from={{ request.get_full_path}}" class="pull-left">忘记密码？</a>
                    <div class="pull-right">
                        <button type="submit" class="btn btn-primary">登录</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div>
      </div>
    </div>

    {% block script_extends %}{% endblock %}

    <script type="text/javascript">
        // 捕获登录框的提交
        $("#modal_login_form").submit(function(event){
            event.preventDefault();
            $.ajax({
                url : "{% url 'modal_login' %}",
                type : "POST",
                data : $(this).serialize(),
                cache : false,
                success : function(data){
                    if(data['status']=='SUCCESS'){
                        // $('#login_modal').modal('hide');
                        window.location.reload();
                    }else{
                        $("#modal_login_error").text(data['message']);
                    }
                },
                error : function(xhr){
                    console.log(xhr);
                }
            });
        })
    </script>
</body>
</html>
